<?php

require_once (APPLICATION_DIR . "/library/external/XOAD/xoad.php");

class AdminbrandController extends DRX_Controller {

    function indexAction() {
        //$this -> listAction();
        global $arrArgs;
        
        $objSmarty = DRX::load('smarty');
        $objBrand = new AlumniBrand();
        $this->runXoadServer();
        $this->pathWeb();

        
        define("RESULT_PER_PAGE_NEWS", 20);
        $intPage = isset($arrArgs[2]) ? $arrArgs[2] : 1;
        $intStart = ($intPage - 1) * RESULT_PER_PAGE_NEWS;
        $objListBrands = $objBrand->getListBrand($intStart, RESULT_PER_PAGE_NEWS);
        //echo 1;die();
        //phan trang
        include "IO_Page.php";
        $intTotal = $objListBrands->count('distinct');
        $objNaviPage = new IO_Page(5, RESULT_PER_PAGE_NEWS, 'onPage');
        $strNaviPage = $objNaviPage->createPageNavigator($intPage, $intTotal, "");
        $objSmarty->assign("NAVIGATION_PAGE", $strNaviPage);
        $objSmarty->assign("TOTAL", $intTotal);
        //echo 1;die();
        $objSmarty->assign("objListBrands", $objListBrands);
        $objSmarty->assign("intStart", $intStart);
        $objSmarty->assign("content", $objSmarty->fetch("admin/brand/index.tpl"));
        $objSmarty->display("admin/admin.tpl");
    }

    function addAction() {
        global $arrArgs;
        $objSmarty = DRX::load('smarty');
        $objBrand = new AlumniBrand();
        $this->runXoadServer();
        $this->pathWeb();

        if (isset($_POST["bInfo"])) {
            $this->uploadImage();
            if($arrArgs[1] > 0){                
                $objBrand->updateBrand($_POST);
            }else{
                $objBrand->addBrand($_POST);
            }
        }
        
        if($arrArgs[1] > 0){
            $brandInfo = $objBrand->getInfoBrand($arrArgs[1]);
            $objSmarty -> assign("brandInfo", $brandInfo);
        }
        $objSmarty->assign("content", $objSmarty->fetch("admin/brand/frm_add.tpl"));
        $objSmarty->display("admin/admin.tpl");
    }
    
    function uploadImage()
	{
		if(isset ($_FILES) && $_FILES['bImage']['name'] != ""){                    
                    //upload hinh
                    include "IO_Upload.php";
                    $strPathUpload = APPLICATION_DIR . '/images/brand';
                    
                    //echo $strPathUpload;
                    $objUpload = new IO_Upload(true, $strPathUpload, 0);
                    $objUpload -> setPrefixThumb(time() . "_");
                    $objUpload -> uploadImage('bImage', $strNameImage);
                    //echo $objUpload -> getMsgError();
                    if ( $strNameImage != '' )
                    {
                            $objUpload -> setPrefixThumb("solvay_");
                            $objUpload -> createThumbnail(100, $strPathUpload, $strNameImage);
                            

                            $_POST['image'] = $strNameImage;
                    }//end if
                    else
                            $_POST['image'] = '';
                }
		
	}
    
    function delAction(){
        global $arrArgs;
        $objBrand = new AlumniBrand();
        if($arrArgs[1] > 0){
            $objBrand->delBrand($arrArgs[1]);
        }
        redirect(APPLICATION_URL . "admin/brand");
    }

    function createFCK($p_strName, $p_strValue = "") {
        $oFCKeditor = new FCKeditor($p_strName);
        $oFCKeditor->BasePath = FCK_BASE_PATH;
        $oFCKeditor->Height = FCK_DEFAULT_HEIGHT;
        $oFCKeditor->Value = $p_strValue;
        $fck = $oFCKeditor->Create();
        return $fck;
    }

    function pathWeb() {
        $objSmarty = DRX::load('smarty');
        $list_path = array();
        $prefix_url = APPLICATION_URL . ADMIN_URL;
        $list_path[0]['url'] = $prefix_url;
        $list_path[0]['title'] = ADMIN_HOME_PATH_WEB_TITLE;
        $last_path['url'] = $prefix_url . "/brand";
        $last_path['title'] = "Alumni Brands";

        $objSmarty->assign("list_path", $list_path);
        $objSmarty->assign("last_path", $last_path);
        $path_web = $objSmarty->fetch("admin/path_web.tpl");
        $objSmarty->assign("path_web", $path_web);
    }

    function runXoadServer() {
        $objSmarty = DRX::load('smarty');

        if (XOAD_Server::runServer()) {
            exit();
        }

        $objSmarty->assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL . "library/external/XOAD"));
        $objSmarty->assign("xoad_handler", XOAD_Client::register($this, "/Adminevent"));
    }

}

?>